package com.alfred.bus.controller;


import com.alfred.bus.service.CheckService;
import com.alfred.bus.service.CustomerService;
import com.alfred.bus.service.RentService;
import com.alfred.bus.vo.CheckVo;
import com.alfred.bus.vo.RentVo;
import com.alfred.sys.constant.SysConstant;
import com.alfred.sys.domain.User;
import com.alfred.sys.utils.DataGridView;
import com.alfred.sys.utils.RandomUtils;
import com.alfred.sys.utils.ResultObj;
import com.alfred.sys.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * @author 张彤彤
 * 用户端功能
 */
@Controller
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private RentService rentService;

    @RequestMapping("toRentCar")
    public String toRentCarManager(Model model){
        User user = (User) WebUtils.getHttpSession().getAttribute("user");
        model.addAttribute("identity",user.getIdentity());
        return "business/client/rentCarClient";
    }
    @RequestMapping("/toRentRecord")
    public String toRentRecord(){
        return "business/client/rentClient";
    }

    @RequestMapping("/toReturnCar")
    public String toCheckCarManager(){
        return "business/client/checkReturnCar";
    }

    @RequestMapping("/toCheckRecord")
    public String toCheckRecord(){
        return "business/client/checkRecord";
    }


    @Autowired
    private CheckService checkService;


    @ResponseBody
    @RequestMapping("saveCheck")
    public ResultObj saveCheck(CheckVo checkVo){
        try{
            User user = (User) WebUtils.getHttpSession().getAttribute("user");
            checkVo.setUserid(user.getUserid());
            checkVo.setCreatetime(new Date());
            this.checkService.addCheckByClent(checkVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 根据出租单号加载检查单的表单数据
     * @param checkid
     * @return
     */
    @ResponseBody
    @RequestMapping("initCheckFormData")
    public Map<String,Object> initCheckFormData(String checkid){
        return this.checkService.initCheckClientFormData(checkid);
    }


    /**
     * 查询我的检查单
     * @param checkVo
     * @return
     */
    @RequestMapping("loadMyCheck")
    @ResponseBody
    public DataGridView loadAllCheck(CheckVo checkVo){
        User user = (User) WebUtils.getHttpSession().getAttribute("user");
        checkVo.setUserid(user.getUserid());
        return this.checkService.queryAllCheck(checkVo);
    }

    @ResponseBody
    @RequestMapping("saveRent")
    public ResultObj saveRent(RentVo rentVo) {
        try {
            //设置创建时间
            rentVo.setCreatetime(new Date());
            //设置归还状态
            rentVo.setRentflag(SysConstant.RENT_BACK_FALSE);
            Long starTime=rentVo.getBegindate().getTime();
            Long endTime=rentVo.getReturndate().getTime();
            Long num=endTime-starTime;//时间戳相差的毫秒数
            num=num/24/60/60/1000;
            Double price=rentVo.getPrice()*num.doubleValue();
            rentVo.setPrice(price);
            //保存
            this.rentService.addRent(rentVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }



    @RequestMapping("loadMyRent")
    @ResponseBody
    public DataGridView loadMyRent(RentVo rentVo){
        User user = (User) WebUtils.getHttpSession().getAttribute("user");
        rentVo.setIdentity(user.getIdentity());
        return this.rentService.queryAllRent(rentVo);
    }

    /**
     * 初始化添加出租单的表单的数据
     * @param rentVo
     * @return
     */
    @ResponseBody
    @RequestMapping("initRentFrom")
    public RentVo initRentFrom(RentVo rentVo){
        //生成出租单号
        rentVo.setRentid(RandomUtils.createRandomStringUseTime(SysConstant.CAR_ORDER_CZ));
        //设置起租时间
        rentVo.setBegindate(new Date());
        //设置操作员
        User user = (User) WebUtils.getHttpSession().getAttribute("user");
        rentVo.setOpername(user.getRealname());
        return rentVo;
    }

}
